<template>
  <div class="approval-container">
    <div class="top">
      <el-row type="flex" justify="left">
        <div class="top_item">
          <label class="label">对方账户名</label>
          <el-input v-model="accountName" type="text" placeholder="请输入户名" class="input" clearable />
        </div>
        <div class="top_item">
          <label class="label">对方账号</label>
          <el-input v-model="account" type="text" placeholder="请输入账号" class="input" clearable />
        </div>
        <div class="top_item">
          <label class="label">收支方向</label>
          <el-select v-model="direction" clearable>
            <el-option value="收入">收入</el-option>
            <el-option value="支出">支出</el-option>
          </el-select>
        </div>
      </el-row>
      <el-row type="flex" justify="left" style="margin-top:20px">
        <div class="top_item">
          <label class="label">交易时间段</label>
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </div>
        <div class="top_item">
          <label class="label">是否关联</label>
          <el-select v-model="isRelated" clearable>
            <el-option value="是">是</el-option>
            <el-option value="否">否</el-option>
          </el-select>
        </div>
        <div class="top_item">
          <el-button type="primary" icon="el-icon-search" @click="getListInfo()">查询</el-button>
          <el-button @click="reset()"> 重置</el-button>
        </div>
      </el-row>
      <el-row type="flex" justify="left">
        <el-table
          :header-cell-style="{'text-align':'center'}"
          :cell-style="{'text-align':'center'}"
          :data="transInfoData"
          max-height="400px"
        >
          <el-table-column v-if="false" prop="transferId" label="交易编号" />
          <el-table-column prop="ind" label="序号" :formatter="handleTransIndex" />
          <el-table-column prop="bankAccount" label="银行账号" />
          <el-table-column prop="bankAccountName" label="银行账户名" />
          <el-table-column prop="oppBankAccountName" label="对方户名" />
          <el-table-column prop="oppBankAccount" label="对方账号" />
          <el-table-column prop="oppBankAccountName" label="对方账户名" />
          <el-table-column prop="amt" label="交易金额" />
          <el-table-column prop="cdsign" label="收支方向" :formatter="handleSign" />
          <el-table-column prop="relevanceFlag" label="是否关联" :formatter="handleFlag" />
          <el-table-column prop="transTime" label="交易时间" :formatter="handleDate" />
          <el-table-column prop="abs" label="备注" />
          <el-table-column label="操作" width="200px">
            <template slot-scope="scope">
              <el-button v-if="scope.row.relevanceFlag !== '1'" type="primary" size="small" @click="neglect(scope.row.transferId)">忽略</el-button>
              <el-button v-if="scope.row.relevanceFlag === '1'" size="small" @click="relevanceCheck(scope.row.transferId)">解除关联</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row type="flex" justify="center">
        <el-pagination
          :page-size="transPageSize"
          :current-page="transCurrentPage"
          :page-sizes="[5,10,20,50]"
          layout="sizes,prev,pager,next,total"
          :total="transTotalCount"
          @size-change="handleTransInfoSizechange"
          @current-change="handleTransInfoCurrentchange"
        />
      </el-row>
    </div>
  </div>
</template>
<script>
import transApi from '@/api/trans.js'
import compApi from '@/api/compApi.js'
export default {
  data() {
    return {
      isRelated: '',
      accountName: '',
      account: '',
      direction: '',
      dateRange: [],
      transInfoData: [],
      transPageSize: 5,
      transCurrentPage: 1,
      transTotalCount: 0
    }
  },
  created() {
    this.getListInfo()
  },
  methods: {
    reset: function() {
      this.accountName = ''
      this.account = ''
      this.direction = ''
      this.dateRange = null
      this.isRelated = ''
    },
    getListInfo: function() {
      this.transCurrentPage = 1
      this.getTransListInfo()
    },
    getTransListInfo: function() {
      var direc = ''
      if (this.direction === '收入') {
        direc = 0
      } else if (this.direction === '支出') {
        direc = 1
      }
      var startDate, endDate
      if (this.dateRange == null || this.dateRange.length < 2) {
        startDate = ''
      } else {
        startDate = this.dateRange[0]
      }
      if (this.dateRange == null || this.dateRange.length < 2) {
        endDate = ''
      } else {
        endDate = this.dateRange[1]
      }
      var related
      if (this.isRelated == null || this.isRelated === '') {
        related = ''
      } else {
        related = this.isRelated === '是' ? '1' : '0'
      }
      const params = {
        accountName: this.accountName,
        account: this.account,
        direction: direc,
        startDate: startDate,
        endDate: endDate,
        isRelated: related,
        pageSize: this.transPageSize,
        currentPage: this.transCurrentPage
      }
      transApi.searchTransInfoList(params).then(res => {
        if (res.retCode === '0000') {
          this.transInfoData = res.list
          this.transTotalCount = res.pageInfo.totalSize
        } else {
          this.$message.error('获取交易信息失败，原因' + res.retMsg)
        }
      })
    },
    relevanceCheck: function(relevanceId) {
      var list = []
      list.push(relevanceId)
      const params = {
        payInfoId: '',
        transferIdList: JSON.stringify(list),
        flag: '0'
      }
      this.$confirm('确定取消关联?', '确认消息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        compApi.relevanceCheck(params).then(res => {
          if (res.retCode === '0000') {
            this.$message.success('取消关联成功')
            this.getTransListInfo()
          } else {
            this.$message.error('取消关联失败，原因' + res.retMsg)
          }
        })
      }).catch(() => {
        this.$message.info('取消成功')
      })
    },
    neglect: function(id) {
      const params = {
        transferId: id
      }
      this.$confirm('确定忽略?', '确认消息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        transApi.neglectTransferInfo(params).then(res => {
          if (res.retCode === '0000') {
            this.$message.success('忽略交易明细成功')
            this.transCurrentPage = 1
            this.getTransListInfo()
          } else {
            this.$message.error('忽略交易明细失败，原因' + res.retMsg)
          }
        })
      }).catch(() => {
        this.$message.info('取消忽略成功')
      })
    },
    handleTransIndex: function(row, column, cellValue, index) {
      return (index + 1) + (this.transCurrentPage - 1) * this.transPageSize
    },
    handleDate: function(row, column, cellValue, index) {
      return cellValue.substr(0, 10)
    },
    handleFlag: function(row, column, cellValue, index) {
      return cellValue === '0' ? '否' : '是'
    },
    handleSign: function(row, column, cellValue, index) {
      return cellValue === '0' ? '收入' : '支出'
    },
    handleTransInfoSizechange: function(val) {
      this.transPageSize = val
      this.getListInfo()
    },
    handleTransInfoCurrentchange: function(val) {
      this.transCurrentPage = val
      this.getTransListInfo()
    }
  }
}
</script>
<style>
/* 顶部条件筛选栏*/
  .top {
    margin-top: 10px;
    margin-bottom: 10px;
    padding-top: 20px;
    padding-bottom: 20px;
    width: 100%;
    border:1px solid  rgba(0,0,0,0.2);
    box-shadow:1px 1px 2px 0px rgba(0,0,0,0.2);
  }
  .top_item{
    width: auto;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-left: 10px;
  }
  .top_item label {
    width: auto;
    min-width: fit-content;
    margin:10px;
  }
  .top_item input {
    width: auto;
    min-width: fit-content;
  }
</style>
<style lang="scss" scoped>
.approval {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
